﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common.ClientInfo
{
    /// <summary>
    /// 获取BS客户端请求的信息
    /// </summary>
    public class ClientHelper
    {

        /// <summary>
        /// 获取页面内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetWebContent(string url)
        {
            var wc = new WebClient { Credentials = CredentialCache.DefaultCredentials, Encoding = Encoding.UTF8 };
            var returnText = wc.DownloadString(url);
            return returnText;
        }

        public static string PostWeb(string postUrl, string postData, Encoding dataEncode, out string msg)
        {
            string ret = string.Empty;
            msg = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(postData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/Json"; //json

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }

            return ret;
        }

        public static string PostWeb(string postUrl, byte[] postData, Encoding dataEncode, string contentType,
            out string msg)
        {
            string ret = string.Empty;
            msg = string.Empty;
            try
            {
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = contentType;

                webReq.ContentLength = postData.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(postData, 0, postData.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }

            return ret;
        }

        public static string PostWeb(string postUrl, string filepath, out string msg)
        {
            var result = string.Empty;
            msg = string.Empty;
            var myWebClient = new WebClient { Credentials = CredentialCache.DefaultCredentials };
            try
            {
                byte[] responseArray = myWebClient.UploadFile(postUrl, "POST", filepath);
                result = Encoding.Default.GetString(responseArray, 0, responseArray.Length);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return result;
        }
    }
}
